<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding;

use app\api\controller\Controller;
use app\api\model\ding\Food as FoodModel;
use app\api\model\ding\FoodCategory as FoodCategoryModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 购物车管理
 * Class Cart
 * @package app\api\controller
 */
class Food extends Controller {
	public function foodList(int $shopId = 0): Json {

		$service = new FoodModel;

		$list = $service->getList($shopId);

		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \cores\exception\BaseException
	 */
	public function list(int $shopId=0): Json {
		
		$service = new FoodCategoryModel;

		$list = $service->getListShop($shopId);

		return $this->renderSuccess(compact('list'));
	}
	public function delete(int $foodId): Json {
		$model = FoodModel::detail($foodId);

		$model->del();
		return $this->renderSuccess([], '保存成功');
	}
	public function detail(int $foodId): Json {
		$detail = FoodModel::detail($foodId, ['image']);

		return $this->renderSuccess(compact('detail'));
	}
	public function add(int $shopId=0): Json {
		$model = new FoodModel;
	
		$model->add($shopId, $this->postForm());
		return $this->renderSuccess([], '保存成功');
	}
	public function edit(int $foodId): Json {

		$detail = FoodModel::detail($foodId);
		$detail->edit($this->postForm());
		return $this->renderSuccess([], '保存成功');
	}

	// public function edit(): Json {
	// 	$model = new FoodCategoryModel;
	// 	$user = UserService::getCurrentLoginUser(true);
	// 	$shopId = Db::name('store_shop_clerk')->where('user_id', $user['user_id'])->value('shop_id');
	// 	$model->add($shopId, $this->postForm());
	// 	return $this->renderSuccess([], '保存成功');
	// }
	public function categoryDelete($categoryId): Json {
		Db::name('ding_food_category')->where('category_id', $categoryId)->delete();
		Db::name('ding_food')->where('category_id',$categoryId)->delete();
		return $this->renderSuccess([], '保存成功');
	}
	public function category_add(int $shopId=0): Json {
		$model = new FoodCategoryModel;

		$model->add($shopId, $this->postForm());
		return $this->renderSuccess([], '保存成功');
	}

}
